var taskId,roleType,userId,userPickerName;
mui.init({
	beforeback: function() {
	　　 //获得父页面的webview
		var list = plus.webview.currentWebview().opener();
	　　 //触发父页面的自定义事件(refresh),从而进行刷新
		mui.fire(list, 'refresh');
		//返回true,继续页面关闭逻辑
		return true;
	}
});

var siteClassDetailIds,checkResults;
var imgArr = [];
mui.plusReady(function(){
	self = plus.webview.currentWebview();
	taskId = self.info.taskId;
	siteId = self.info.siteId;
	userId = localStorage.getItem("userId");
	roleType = localStorage.getItem("roleType");
	getTaskDetail();
	setTimeout(function(){
		if(roleType==2){
			document.getElementById('handleNotes').style.display = "none";
			document.getElementById('sanm-type').style.display = "none";
			request("/app/alarm/scan/site/task/"+siteId+"/"+userId, "GET",{
			}, function(res) {
				plus.nativeUI.closeWaiting();
				if(res.code==0){
					for(var i=0,len=res.result.length;i<len;i++){
						//siteClassDetailIds.push(res.result[i].id)
						for(var j=0,lens=siteClassDetailIds.length;j<lens;j++){
							if(res.result[i].id==siteClassDetailIds[j]){
								res.result[i].checkVal = checkResults[j]
							}
						}
					}
					setTimeout(function(){
						render('#list_wrap', 'taskList', res);
					},100)
				}
				else{
					
				}
			}, true, function() {

			});
		}
		else if(roleType==3){
			document.getElementById('sanm-type').style.display = "block";
			request("/app/alarm/scan/site/task/"+taskId, "GET",{
			}, function(res) {
				plus.nativeUI.closeWaiting();
				if(res.code==0){
					for(var i=0,len=res.result.length;i<len;i++){
						//siteClassDetailIds.push(res.result[i].id)
						for(var j=0,lens=siteClassDetailIds.length;j<lens;j++){
							if(res.result[i].id==siteClassDetailIds[j]){
								res.result[i].checkVal = checkResults[j]
							}
						}
					}
					setTimeout(function(){
						render('#list_wrap', 'taskList', res);
					},100)
				}
				else{
					
				}
			}, true, function() {

			});
		}
	}, 500)
})

function getTaskDetail(){
	request("/app/alarm/inspect/task/site/detail/" + taskId + "/" + userId + "/" + roleType, "GET",{}, function(res) {
//		appPage.endPullRefresh();
		console.log(JSON.stringify(res))
		var userArr = [];
		var taskDetailId = res.result.taskDetailId;
		siteClassDetailIds = res.result.siteClassDetailIds.split(",");
		checkResults = res.result.checkResults.split(",");
		render('#myinfo_warp', 'taskInfoList', res.result);
		if (res.result.dealStatus !== 2){
			document.getElementById('reportBtn').addEventListener('tap', function(){
				var noteVal = document.getElementById('notes').value;
				if (noteVal == ''){
					mui.toast('请填写处理说明');
					return;
				}
				request("/app/alarm/add/finish/task", "POST",{
					dealInfo: noteVal,
					taskDetailId: taskDetailId,
					userId: userId
				}, function(res) {
					mui.toast('任务处理完成');
					if(res.code==0){
						mui.back();
					}
					else{
						mui.toast(res.desc)
					}
				}, true, function() {
			//		appPage.endPullRefresh();
				});
			})
		}
		
		request("/app/alarm/select/user/list/" + roleType + "/" + userId, "GET",{}, function(result) {
			
			for (var i=0;i<result.result.length; i++) {
				userArr.push({
					value: result.result[i].id,
					text: result.result[i].userName
				})
			}
			if (res.result.dealStatus !== 2){
				var userPicker = new mui.PopPicker();
				userPicker.setData(userArr);
				var showUserPickerButton = document.getElementById('showUserPicker');
				showUserPickerButton.addEventListener('tap', function(event) {
					userPicker.show(function(items) {
						var btnArray = ['取消', '确定'];
						mui.confirm('确认指派给'+items[0].text + '吗？', '提示', btnArray, function(e) {
							if (e.index == 1) {
								request("/app/alarm/distribute/user", "POST",{
									userId: userId,
									baseUserId: items[0].value,
									taskDetailId: taskDetailId
								}, function(res) {
									mui.toast('指派成功');
									document.getElementById('selectUserName').innerText = items[0].text;
									showUserPickerButton.style.display = 'none';
									var showUserNameCont = document.getElementById('showSelect');
									showUserNameCont.style.display = 'block';
								}, true, function() {
							//		appPage.endPullRefresh();
								});
							} else {
								
							}
						})
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
			}
			
		}, true, function() {
			
		});
		
	}, true, function() {
//		appPage.endPullRefresh();
	});
}